package com.itheima.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-13 14:19
 * @description
 */
@Controller
@RequestMapping("/file")
public class demo4_FileController {

    @RequestMapping("/upload2")
    @ResponseBody  //@ResponseBody的作用其实是将java对象转为json格式的数据
    public String upload(
            //参数绑定
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("avatar") MultipartFile avatar,
            @RequestParam("idcard") MultipartFile idcard
    ) throws IOException {
        System.out.println("用户名:"+username);
        System.out.println("密码:"+username);
        System.out.println("头像:"+avatar.getName());
        System.out.println("头像:"+avatar.getContentType());
        System.out.println("头像:"+avatar.getSize());
        InputStream inputStream = avatar.getInputStream();
        System.out.println("获取头像的文件流:"+inputStream);

        //记得关流
        //记得关流
        //记得关流
        //记得关流
        //记得关流
        inputStream.close();


        //我想保存某个位置上去
        avatar.transferTo(new File("/Users/huyoufu/课程文件/黑马32/springmvc/springmvc02/xx.png"));

        return "success";
    }
}
